<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/4/9
 * Time: 17:06
 */

namespace app\admin\controller;

use app\admin\logic\MessageLogic;
use app\common\controller\Backend;
use app\common\utils\Constant;
use think\Db;
use think\Exception;


/**
 * 房产管理
 * Class Houseproperty
 * @package app\admin\controller
 */
class Houseproperty extends Backend
{
    protected $model = null;
    protected $relationSearch = true;

    public function _initialize(){
        parent::_initialize();
        $this->model = model('HouseProperty');
    }

    /**
     * 查看
     * @param $ids='' 业主id
     * @return mixed
     */
    public function index($ids='')
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $user_id = $this->auth->id;
            $user = model('User')->where(array('id'=>$user_id))->field('user_type,property_id')->find();
            $map['owner_id'] = $ids;
            if($user['user_type'] == Constant::USER_TYPE_PROPERTY){
                $village_ids = $this->getVillageIds($user['property_id']);
                $map['village_id'] = ['in',$village_ids];
            }

            $total = $this->model
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->count();
//            $this->error($this->model->getLastSql());
            $list = $this->model
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $key => &$value){
                $value['village_name'] = model('Village')->where("id={$value['village_id']}")->value('name');
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $this->assignconfig('owner_id',$ids);
        return $this->view->fetch();
    }


    /**
     * 添加
     * @param $ids='' 业主id
     * @return mixed
     */
    public function add($ids='')
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                $params['owner_id'] = $ids;
                $params['charge_total'] = $params['water_charge'] + $params['property_charge'];
                $params['create_time'] = time();
                $this->model->create($params);
                $this->success();
            }
            $this->error();
        }
        $this->assignconfig('owner_id',$ids);
        return $this->view->fetch();
    }

    /**
     * 编辑
     * @param $ids='' 房产id
     * @return mixed
     */
    public function edit($ids = '')
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                $params['charge_total'] = $params['water_charge'] + $params['property_charge'];
                $row->save($params);
                $this->success();
            }
            $this->error();
        }
        $village = model('Village')
            ->where(array('id'=>$row['village_id']))
            ->field('province_id,county_id,city_id')
            ->find();
        $this->assignconfig("house_property", $row);
        $this->assignconfig("village", $village);
        return $this->view->fetch();
    }


    /**
     * 删除
     * @param $ids='' 房产id
     * @return mixed
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            Db::startTrans();
            try {
                $result = $this->model->destroy($ids);
                if($result){
                    Db::commit();
                    $this->success('删除成功!');
                }else {
                    $this->error('删除失败!');
                }
            }catch (Exception $e){
                Db::rollback();
                $this->error('删除失败!');
            }

        }
        $this->error();
    }

    public function dept($ids){
        MessageLogic::getInstance()->sendMessageByHousePropertyPay($ids);
        $this->success('发送催款信息成功');
    }
}